package com.eningqu.aipen.ui.adapter;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.eningqu.aipen.demo.R;
import com.eningqu.aipen.demo.databinding.ItemMeidcalNodeBinding;
import com.eningqu.aipen.logic.model.medical.MedicalBookBean;
import com.eningqu.aipen.logic.model.medical.MedicalNodeWithXyModelBean;
import com.eningqu.aipen.ui.medical.MedicalDetailActivity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Package:com.eningqu.aipen.ui.adapter
 * Author:starr
 * Time:2023/11/16 14:48
 * Description: 患者病历节点的适配器
 */
public class MedicalNodeAdapter extends RecyclerView.Adapter<MedicalNodeAdapter.MedicalNodeViewHolder> {

    private List<MedicalNodeWithXyModelBean.JdListDTO> list;
    private MedicalBookBean.PageDTO patient;


    public MedicalNodeAdapter(List<MedicalNodeWithXyModelBean.JdListDTO> list, MedicalBookBean.PageDTO patient) {
        this.list = list;
        this.patient = patient;
    }

    @NonNull
    @Override
    public MedicalNodeViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        ItemMeidcalNodeBinding binding = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), R.layout.item_meidcal_node, viewGroup, false);
        MedicalNodeViewHolder holder = new MedicalNodeViewHolder(binding.getRoot());
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MedicalNodeViewHolder holder, int i) {
        MedicalNodeWithXyModelBean.JdListDTO medical = list.get(i);
        ItemMeidcalNodeBinding binding = DataBindingUtil.getBinding(holder.itemView);
        if (medical.modelEntity == null) {
            return;
        }
        if (medical.modelEntity.emrXyDate != null) {
            binding.tvDate.setText(medical.modelEntity.emrXyDate.substring(0, 10));
        }
        binding.tvBefore1.setText(medical.modelEntity.emrXyBefore);
        binding.tvChief1.setText(medical.modelEntity.emrXyMain);
        binding.tvCreatePeople.setText(medical.modelEntity.emrYsName);
        binding.tvNow1.setText(medical.modelEntity.emrXyNow);

        binding.tvTime.setText(medical.modelEntity.emrXyDate.substring(0, 10));
        binding.clItemRoot.setOnClickListener(v -> {
            //该病程的详情页面
            Bundle bundle = new Bundle();
            bundle.putSerializable("nodeBean", medical.modelEntity);
            bundle.putSerializable("patient", patient);
            MedicalDetailActivity.startAction(v.getContext(), bundle);
        });

        binding.rvNine.setLayoutManager(new GridLayoutManager(binding.getRoot().getContext(), 3));
        ArrayList<String> list = new ArrayList<>();
        if (medical.modelEntity.emrXyImgs != null) {
            String[] strs = medical.modelEntity.emrXyImgs.split(";");
            Collections.addAll(list, strs);
        }
        GridNineAdapter adapter = new GridNineAdapter(list);
        binding.rvNine.setAdapter(adapter);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    protected static class MedicalNodeViewHolder extends RecyclerView.ViewHolder {
        public MedicalNodeViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}
